use Purple;
#use threads;

%PLUGIN_INFO = (
    perl_api_version => 2,
    name => "pidgin-lifestream",
    version => "0.1",
    summary => "Plugin to post status changes to web",
    description => "Posts account status changes to the web for use with the Wordpress Lifestream plugin",
    author => "Jim Hribar <me\@jimhribar.com>",    
    url => "http://code.google.com/p/pidgin-lifestream/",
    load => "plugin_load",
    unload => "plugin_unload"
);
sub plugin_init {
    return %PLUGIN_INFO;
}
sub plugin_load {
    my $plugin = shift;
    Purple::Debug::info("testplugin", "plugin_load() - Test Plugin Loaded.\n");
    # User data to be given as an argument to the callback perl subroutine.
    $data = "";
    # A pointer to the handle to which the signal belongs needed by the callback function
    $accounts_handle = Purple::Accounts::get_handle();
    # Connect the perl subroutine 'signal_cb' to the event 'account-connecting'
    Purple::Signal::connect($accounts_handle, "account-connecting", $plugin, \&signal_cb, $data);
	Purple::Signal::connect($accounts_handle, "account-status-changed", $plugin, \&signal_cb_account_status_changed, $data);	
}
sub plugin_unload {
    my $plugin = shift;
    Purple::Debug::info("testplugin", "plugin_unload() - Test Plugin Unloaded.\n");
}
sub signal_cb {
    # The signal data and the user data come in as arguments
    my ($account, $data) = @_;
	
	#use Thread; 	
    #$thr = new Thread \&sub1, $account->get_protocol_id(), $account->get_username(), "connected";     
    #$thr->detach; 		
	
    Purple::Debug::info("testplugin", "Account \"" . $account->get_username() . "\" just connected.\n");
}
sub signal_cb_account_status_changed {
    # The signal data and the user data come in as arguments
    my ($account, $old, $new) = @_;
	
	use Thread; 
    $thr = new Thread \&sub1, $account->get_protocol_id(), $account->get_username(), $new->get_name(), Purple::SavedStatus::get_current()->get_message();     
    $thr->detach; 	
	
    Purple::Debug::info("testplugin", "Status: " . $new->get_name() . "\n");
}

sub sub1 { 
	my $protocol = shift @_; 
	my $username = shift @_; 
	my $status = shift @_; 
	my $message = shift @_; 

	use LWP::Simple qw($ua get); 
	$ua->proxy('http','http://localhost:3128'); 	
	my $page = get "http://www.jimhribar.com/?protocol=" . $protocol . "&username=" . $username . "&status=" . $status . "&message=" . $message;
}